#include <stdlib.h>
#include <string.h>

#include "descriptions.h"

#include "constantes_et_macros.h"





/* ======================== objets =============================== */


void init_tab_dyn_description_objets(struct tab_dyn_description_objets* tab,
                                     const unsigned int taille)
{
    tab->nb_elements = 0;
    if(taille > 0)
    {
        if(!(tab->tab = (struct description_objet*)malloc
                (taille * sizeof(struct description_objet))))
           exit(EXIT_MALLOC);
    }
    else
        tab->tab = NULL;
}



void ajouter_description_objet(struct tab_dyn_description_objets* tab,
                               const struct description_objet* obj)
{
    struct description_objet* emplacement_nouvel_objet;

    emplacement_nouvel_objet = tab->tab + tab->nb_elements;
    emplacement_nouvel_objet->id_projectile = obj->id_projectile;
    strcpy(emplacement_nouvel_objet->nom, obj->nom);
    emplacement_nouvel_objet->prix_vente = obj->prix_vente;
    emplacement_nouvel_objet->puissance = obj->puissance;
    emplacement_nouvel_objet->pv = obj->pv;
    emplacement_nouvel_objet->type = obj->type;
    emplacement_nouvel_objet->vitesse_charge = obj->vitesse_charge;
    emplacement_nouvel_objet->vitesse_deplacement = obj->vitesse_deplacement;

    tab->nb_elements++;
}



void detruire_tab_dyn_description_objets(struct tab_dyn_description_objets* tab)
{
    if(tab->nb_elements > 0)
    {
        free(tab->tab);
        tab->nb_elements = 0;
    }
}


/* ======================== projectiles =============================== */


void init_tab_dyn_description_projectiles(struct tab_dyn_description_projectiles* tab,
                                          const unsigned int taille)
{
    tab->nb_elements = 0;
    if(taille > 0)
    {
        if(!(tab->tab = (struct description_projectile*)malloc
                (taille * sizeof(struct description_projectile))))
            exit(EXIT_MALLOC);
    }
    else
        tab->tab = NULL;
}



void ajouter_description_projectile(struct tab_dyn_description_projectiles* tab,
                                    const struct description_projectile* proj)
{
    tab->tab[tab->nb_elements] = *proj;
    tab->nb_elements++;
}



void detruire_tab_dyn_description_projectiles(struct tab_dyn_description_projectiles* tab)
{
    if(tab->nb_elements > 0)
    {
        free(tab->tab);
        tab->nb_elements = 0;
    }
}



/* ======================== ennemis =============================== */


void init_tab_dyn_description_ennemis(struct tab_dyn_description_ennemis* tab,
                                      const unsigned int taille)
{
    tab->nb_elements = 0;
    if(taille > 0)
    {
        if(!(tab->tab = (struct description_ennemi*)malloc
                (taille * sizeof(struct description_ennemi))))
            exit(EXIT_MALLOC);
    }
    else
        tab->tab = NULL;
}



void ajouter_description_ennemi(struct tab_dyn_description_ennemis* tab,
                                const struct description_ennemi* enn)
{
    tab->tab[tab->nb_elements] = *enn;
    tab->nb_elements++;
}



void detruire_tab_dyn_description_ennemis(struct tab_dyn_description_ennemis* tab)
{
    int i;

    if(tab->nb_elements > 0)
    {
        for(i = 0;  i < tab->nb_elements;  i++)
        {
            detruire_tab_dyn_id(&tab->tab[i].drops);
            detruire_tab_dyn_id(&tab->tab[i].probas);
        }
        free(tab->tab);
        tab->nb_elements = 0;
    }
}



/* ======================== quetes =============================== */


void init_tab_dyn_quetes(struct tab_dyn_quetes* quetes,
                     const unsigned int nb_quetes)
{
    if(!(quetes->tab = (struct description_quete*)
         malloc(nb_quetes * sizeof(struct description_quete))))
        exit(EXIT_MALLOC);
    quetes->nb_elements = 0;
}



void init_prochaine_quete(struct tab_dyn_quetes* quetes,
                          const unsigned int recompense)
{
    struct description_quete* p_quete;

    p_quete = quetes->tab + quetes->nb_elements;
    p_quete->recompense = recompense;
    init_inventaire(&p_quete->objets_necessaires);
    quetes->nb_elements++;
}



void modifier_derniere_quete(struct tab_dyn_quetes* quetes,
                             const unsigned int id_objet,
                             const unsigned int quantite)
{
    ajouter_exemplaires(&quetes->tab[quetes->nb_elements - 1].objets_necessaires,
                        id_objet, quantite);
}



void detruire_tab_dyn_quetes(struct tab_dyn_quetes* quetes)
{
    int i;

    for(i = 0;  i < quetes->nb_elements;  i++)
        detruire_inventaire(&quetes->tab[i].objets_necessaires);
    if(quetes->nb_elements > 0)
        free(quetes->tab);
}


/* ======================== regions =============================== */



void init_tab_dyn_regions(struct tab_dyn_regions* tab, const unsigned int taille)
{
    if(!(tab->tab = (struct region*)malloc(taille * sizeof(struct region))))
        exit(EXIT_MALLOC);
    tab->nb_elements = 0;
}



void ajouter_region(struct tab_dyn_regions* tab, const unsigned int id,
                    const char* nom,
                    const unsigned int coord_x, const unsigned int coord_y)
{
    struct region* p_region;

    p_region = tab->tab + tab->nb_elements;
    p_region->id = id;
    strcpy(p_region->nom, nom);
    p_region->coord_x = coord_x;
    p_region->coord_y = coord_y;
    tab->nb_elements++;
}



void detruire_tab_dyn_regions(struct tab_dyn_regions* tab)
{
    free(tab->tab);
}






